/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Item;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Trung Duc
 */
public class ItemTableModel extends AbstractTableModel {
    private List<Item> listItem;
    private ResourceBundle messages;
    RegularExpression reg;
    ItemUpdateListener updateListener;
    
    public ItemTableModel(List<Item> listItem,ResourceBundle messages) {
        this.listItem = listItem;
        this.messages = messages;
    
    }
    public boolean addItem(Item item){
        for (Iterator<Item> it = listItem.iterator(); it.hasNext();) {
            Item item1 = it.next();
            if(item1.getName().equalsIgnoreCase(item.getName())){
                return false;
            }
        }
        listItem.add(item);
        return true;
    }
    public void reduceQuantityItem(int id,int quantity){
        for(int i=0;i<listItem.size();i++){
            if(listItem.get(i).getId() == id){
                listItem.get(i).setQuantity(listItem.get(i).getQuantity() - quantity);
            }
        }
    }
    public Item findItem(int id){
        for (Iterator<Item> it = listItem.iterator(); it.hasNext();) {
            Item item = it.next();
            if(item.getId() == id)
                return item;
        }
        return null;
    }
    public boolean checkCategoryName(String categoryName){
        String pattern = messages.getString("patternCategoryName");
        reg = new RegularExpression(pattern);
        return reg.matches(categoryName);
    }
    
    public boolean checkItemName(String itemName){
        String pattern = messages.getString("patternItemName");
        reg = new RegularExpression(pattern);
        return reg.matches(itemName);
    }
    public boolean checkPrice(String price){
        String pattern = messages.getString("patternPrice");
        reg = new RegularExpression(pattern);
        return reg.matches(price);
    }
    public boolean checkUnit(String unit){
        String pattern = messages.getString("patternUnit");
        reg = new RegularExpression(pattern);
        return reg.matches(unit);
    }
    @Override
    public int getRowCount() {
        return listItem.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = listItem.get(rowIndex);
        switch(columnIndex){
            case 0: return item.getName();
            case 1: return item.getQuantity();
            case 2: return item.getPrice();
            case 3: return item.getUnit();
            case 4: return item.getCategory();
            default: return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0: return messages.getString("itemName");
            case 1: return messages.getString("itemQuantity");
            case 2: return messages.getString("itemPrice");
            case 3: return messages.getString("itemUnit");
            case 4: return messages.getString("itemCategory");
            default: return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch(columnIndex){
            case 0: return String.class;
            case 1: return Integer.class;
            case 2: return Float.class;
            case 3: return String.class;
            case 4: return String.class;
            default: return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 2: return true;
            default:return false;
        }
    }

    
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Item item = listItem.get(rowIndex);
        switch(columnIndex){
            case 2:
                if(item.getPrice() != (Float)aValue){
                    String strPrice = String.valueOf((Float)aValue);
                    if(checkPrice(strPrice)){
                        float newPrice = (Float)aValue;
                        int confirm = JOptionPane.showConfirmDialog(null, "Are you sure?");
                        if(confirm == JOptionPane.OK_OPTION){
                            item.setPrice(newPrice);
                        }  
                    }
                    if(updateListener!=null){
                        updateListener.update(item);
                    }
                }  
                break;
        }
    }
    public void setUpdateListener(ItemUpdateListener updateListener){
        this.updateListener = updateListener;
    }
}
